package simplemath.math;

import java.util.ArrayList;

import simplemath.common.Constant;
import simplemath.math.ReturnVal;

public class Function {
	
	static {
		System.loadLibrary("nmath");
	}
	
	private long nativeAddress = 0;
	private byte[] variables;
	private String ftext="";
	
	public Function(){}
	
	public Function(String _ftext) {
		ftext = _ftext;
		jniSetFunctionString(_ftext);
	}
	
	public native int jniSetFunctionString(String _ftext);
	public native int jniCalc(float[] values, ReturnVal rv);
	private native int jniGetDerivative();
	public native int jniGetErrorColumn();
	public native ArrayList<ImageData> jniGetSpace(float[] boundaries, float epsilon);
	public native void jniRelease();
	
	public String getFunctionText() {
		return ftext;
	}

	public byte[] getVariables() {
		return variables;
	}

	public void setVariables(byte[] variables) {
		this.variables = variables;
	}
	
	public String getErrorMsg(int code) {
		String msg;
		
		switch(code) {
			case Constant.ERROR_JNI_ERROR:
				msg = "Some error from native code!";
			break;
			
			case Constant.ERROR_PARENTHESE_MISSING:
				msg = "Missing parenthese at position " + jniGetErrorColumn();
			break;
			
			case Constant.ERROR_BAD_TOKEN:
				msg = "Bad token found at position " + jniGetErrorColumn();
			break;
			
			case Constant.ERROR_NOT_A_FUNCTION:
				msg = "Bad function declaration found at " + jniGetErrorColumn();
			break;
			
			default:
				msg = "Some error occurs.";
			break;
		}
		
		return msg;
	}
}
